<template>
  <div class="app-container">
    <el-button-group style="float: right;padding-bottom: 5px;">
      <el-button type="dark" icon="el-icon-edit" @click="openNewForm()" plain>新增</el-button>
    </el-button-group>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" >
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="机构代码" align="center">
        <template slot-scope="scope">
          {{ scope.row.code }}
        </template>
      </el-table-column>
      <el-table-column label="机构名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="openEditForm(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteData(scope.row)">删除</el-button>
          <el-button type="text" @click="orgTypeEdit(scope.row)">项目配置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 15, 30, 50]"
      :page-size="pageInfo.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total">
    </el-pagination>
    <org-form :drawer="openDrawer" :orgInfo="orgInfo" @closeOrgForm="closeOrgForm"/>
    <org-type-auth :drawer="openAuth" :orgInfo="orgInfo" :data="auths" :valueNow="orgTypeAuths" @close="closeTypesForm"/>
  </div>
</template>

<script>
import {getList, deleteOrg} from '@/api/org'
import orgForm from './orgForm'
import OrgTypeAuth from "@/views/finance/org/orgTypeAuth";
import {selectType} from "@/api/type";
import {getTypes} from "@/api/orgType";

export default {
  components:{
    OrgTypeAuth,
    orgForm,
  },
  filters: {},
  data() {
    return {
      list: null,
      listLoading: true,
      openDrawer: false,
      openAuth:false,
      orgInfo: {
        id: null,
        name: null,
        code: null
      },
      pageInfo: {
        page: 1,
        limit: 10,
        total: 0
      },
      auths:[],
      orgTypeAuths: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.pageInfo).then(response => {
        this.list = response.data.list
        this.pageInfo.total = response.data.total
        this.listLoading = false
      }).catch(()=>{alert("err")})
    },
    openNewForm() {
      this.openDrawer = true
    },
    openEditForm(value) {
      this.orgInfo = value
      this.openDrawer = true
    },
    deleteData(value) {
      deleteOrg(value).then(()=>{
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
        this.fetchData()
      }).catch(() => {
        this.$notify.error({
          title: '错误',
          message: '删除失败'
        })
        this.fetchData()
      })
    },
    closeOrgForm(v) {
      this.openDrawer = v
      this.fetchData()
      this.orgInfo = {}
    },
    handleSizeChange(val) {
      this.pageInfo.limit = val
      this.pageInfo.page = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.fetchData()
    },
    orgTypeEdit(value) {
      this.auths = this.orgAllTypeData()
      this.orgInfo = value
      this.orgTypeAuths = this.orgTypeData(value)
      this.openAuth = true
    },
    orgAllTypeData() {
      const data = [];
      selectType().then(response => {
        response.data.forEach((type) => {
          data.push({
            label: type.typeName,
            key: type.id
          });
        });
      }).catch(()=>{alert("type err")})
      return data;
    },
    orgTypeData(value) {
      getTypes(value).then(response => {
        this.orgTypeAuths = response.data
      }).catch(()=>{alert("type err")})
    },
    closeTypesForm(v) {
      this.openAuth = v
      this.fetchData()
      this.orgTypeAuths = []
      this.orgInfo = {}
    }
  }
}
</script>
